package com.letere.service.imp;

import com.letere.service.BlogService;
import com.letere.service.FileService;
import com.letere.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author letere
 * @create 2021-02-09 17:12
 */
@Service
public class FileServiceImp implements FileService {

    //服务器中资源路径
    private final String basePath = "./src/main/resources/files";
    //资源返回的url地址
    private final String baseUrl = "http://localhost:8081/file";

    @Autowired
    private BlogService blogService;

    /**
     * 新增文件
     * @param id 文件id
     * @param file 文件
     * @return String 文件访问地址
     * @throws Exception 异常
     */
    @Override
    public String addFile(Integer id, MultipartFile file) throws Exception {
        //决定文件夹名
        String folder;
        if(id == null) {
            folder = "/" + blogService.getNextBlogId();
        }else if(id == -1) {
            //关于about的操作
            File aboutFile = new File("./src/main/resources/about/" + file.getOriginalFilename());
            if(!aboutFile.exists()) {
                FileOutputStream fos = new FileOutputStream(aboutFile);
                fos.write(file.getBytes());
                fos.close();
            }
            return "http://localhost:8081/about/" + file.getOriginalFilename();
        }else {
            folder = "/" + id;
        }
        File bf = new File(basePath + folder);

        //判断该文件夹是否存在
        if(!bf.exists()) {
            bf.mkdirs();
        }

        //将文件写出
        String fileName = "/" + file.getOriginalFilename();
        File img = new File(basePath + folder + fileName);
        if(!img.exists()) {
            FileOutputStream fos = new FileOutputStream(img);
            fos.write(file.getBytes());
            fos.close();
        }

        //返回服务器地址
        return baseUrl + folder + fileName;
    }


    /**
     * 删除文件
     * @param id 文件id
     * @param fileUrl 文件服务器地址
     */
    @Override
    public void deleteFile(Integer id, String fileUrl) {
        //决定文件夹
        String folder;
        if(id == null) {
            folder = "/" + blogService.getNextBlogId();
        }else if(id == -1) {
            //关于about的操作
            String[] temp = fileUrl.split("/");
            File aboutFile = new File("./src/main/resources/about/" + temp[temp.length - 1]);
            if(aboutFile.exists()) {
                aboutFile.delete();
            }
            return;
        }else {
            folder = "/" + id;
        }

        File file;
        //判断是否删除一个文件 或 整个文件夹
        if(fileUrl.equals("null")) {
            file = new File(basePath + folder);
        }else {
            String[] temp = fileUrl.split("/");
            file = new File(basePath + folder + "/" + temp[temp.length - 1]);
        }

        //判断文件是否存在
        if(file.exists()) {
            FileUtil.deleteDir(file); //工具类删除文件
        }
    }


    /**
     * 获取"关于"文章中内容
     * @return String 文章内容
     * @throws IOException 异常
     */
    @Override
    public String getAbout() throws IOException {
        String path = "./src/main/resources/about/content.txt";
        FileReader fr = new FileReader(path);
        char[] cbuf = new char[5];
        int len;
        String content = "";
        while((len = fr.read(cbuf)) != -1) {
            content = content + new String(cbuf, 0, len);
        }
        return content;
    }


    /**
     * 更新"关于"文章的内容
     * @param content 文章内容
     * @throws IOException 异常
     */
    @Override
    public void editAbout(String content) throws IOException {
        String path = "./src/main/resources/about/content.txt";
        FileWriter fw = new FileWriter(path);
        fw.write(content);
        fw.close();
    }
}
